package com.sc.outpatientsys.service.impl;

import com.sc.outpatientsys.entity.Student;
import com.sc.outpatientsys.mapper.StudentMapper;
import com.sc.outpatientsys.service.IStudentService;
import com.sc.outpatientsys.service.ex.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;
import java.util.UUID;

@Service
public class StudentServiceImpl implements IStudentService {
    @Autowired
    private StudentMapper studentMapper;
    @Override
    public void reg(Student student) {
        String username = student.getUserName();
        // 调用方法判断是否有用户名已经存在
        Student rst = studentMapper.findByStudentName(username);
        //检测能否找到
        if(rst != null){
            throw new UserNameHasExistException("用户名被占用");
        }
        // 插入
        Integer rows = studentMapper.insertStudent(student);
        if(rows != 1){
            throw new InsertException("用户注册过程中出现未知错误！");
        }

    }

    @Override
    public Student login(String userName, String password) {
        Student result = studentMapper.findByStudentName(userName);
        if (result != null || password.equals(result.getPassword())){
            return result;
        }
        else {
            throw new PasswordNotMacthException("用户名密码错误");
        }
    }

    @Override
    public void update(Student student) {
        Integer rows = studentMapper.updateStudent(student);
        if (rows != 1){
            throw new UpdateException("修改时出现未知错误");
        }
    }

    @Override
    public void delete(int st_ID) {
        Integer rows = studentMapper.deleteStudent(st_ID);
        if (rows != 1){
            throw new DeleteException("删除时出现未知错误");
        }
    }

    @Override
    public Student findByID(int st_ID) {
        Student result = studentMapper.findByStudentID(st_ID);
        if(result == null){
            throw new NotFindException("未找到");
        }
        return result;
    }

    @Override
    public Student findByName(String userName) {
        Student result = studentMapper.findByStudentName(userName);
        if(result == null){
            throw new NotFindException("未找到");
        }
        return result;
    }

    @Override
    public List<Student> findAll() {
        List<Student> result = studentMapper.selectAllStudent();
        if(result == null){
            throw new NotFindException("未找到");
        }
        return result;
    }
}
